package com.utils.wechat;

import com.bean.WebsiteUserInfo;
import com.bean.wechat.ResultInfo;
import com.bean.wechat.WechatAccount;
import com.bean.wechat.WechatMsg;
import com.bean.wechat.WechatWebsite;
import com.mapper.mysql.UserInfoMapper;
import com.mapper.repository.mysql.WechatAccountRepository;
import com.mapper.repository.mysql.WechatMsgRepository;
import com.mapper.repository.mysql.WechatWebsiteRepository;
import com.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class WechatStrategyServer implements WechatStrategy  {

    @Value("${wechat.appid}")
    //微信公众号APPID
    private String account ;

    @Autowired
    private BaseUtils baseUtils;

    @Autowired
    private WechatAccountRepository wechatAccountRepository;

    @Override
    public void returnMsg(ResultInfo resultInfo, String content) {

        if(content.equals("公众号服务")){
            getServer(resultInfo,content);
        }else if(content.startsWith("ID:")){
            bindServer(resultInfo,content);
        }
    }

    //获取微信公众号服务
    public void getServer(ResultInfo resultInfo, String content) {

        String param = baseUtils.getBaseConfigByName("wechat_server");

        String userid = resultInfo.getToUserName();
        WechatAccount wechatAccount = wechatAccountRepository.findByUserid(userid);

        //查询当前用户是咨询过，咨询过就不在进行保存。
        if(wechatAccount!=null){
            resultInfo.setReturnMethod("text");
            resultInfo.setContent("ID:"+wechatAccount.getId()+"，你需要结果为:"+param);
        }else{

            //判断这个问题是否来自 主体公众号，如果是就保存信息，如果不是就要求用户关注主体公众号

            if(resultInfo.getFromUserName().equals(account)){
                wechatAccount = new WechatAccount();
                wechatAccount.setId(UUID.randomUUID().toString());
                wechatAccount.setUserid(resultInfo.getToUserName());
                wechatAccount.setCreatetime(LocalDateTime.now());
                wechatAccountRepository.save(wechatAccount);//咨询后就将用户信息存储

                resultInfo.setReturnMethod("text");
                resultInfo.setContent("ID:"+wechatAccount.getId()+"，你需要结果为:"+param);
            }else{
                resultInfo.setReturnMethod("text");
                resultInfo.setContent("请关注公众号:‘软件漫游指南’进行咨询获取");
            }
        }
    }

    //绑定微信公众号服务
    public void bindServer(ResultInfo resultInfo, String content) {

        String id = content.substring("ID:".length());
        WechatAccount wechatAccount = wechatAccountRepository.getById(id);
        if(wechatAccount==null){
            resultInfo.setReturnMethod("text");
            resultInfo.setContent("回复的ID无效，"+"请关注公众号:‘软件漫游指南’进行咨询获取");
        }else if(wechatAccount.getAccount()!=null){
            resultInfo.setReturnMethod("text");
            resultInfo.setContent("请勿重新发送ID");
        }else{
            String accountId = resultInfo.getFromUserName();
            wechatAccount.setAccount(accountId);
            wechatAccountRepository.save(wechatAccount);
            resultInfo.setReturnMethod("text");
            resultInfo.setContent("请使用");
        }

    }
}
